﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using MINERP.MODEL.FoundationModel;
using MINERP.BLL.DisjoinProForOutShop;

namespace MINERP.DisjoinProForOutShop.Forms
{
    public partial class FrmReCheck : Form
    {
        public FrmReCheck()
        {
            InitializeComponent();
        }
        List<OrderInfo_Info> orderInfoList = new List<OrderInfo_Info>();
        DisJoinBLL2_Fuji _bll = new DisJoinBLL2_Fuji();
        Shop_Info shopInfo = new Shop_Info();
        private void textEdit1_KeyDown(object sender, KeyEventArgs e)
        {
            if (e.KeyCode == Keys.Enter)
            {
                string strOiBarcode = textEdit1.Text.Trim();

                if (strOiBarcode.Trim().Length == 0)
                {
                    return;
                }
                int orderproductid = Convert.ToInt32(strOiBarcode);
                if (strOiBarcode.Length != 10)
                {
                    strOiBarcode = "2" + string.Format("{0:d9}", Convert.ToInt32(strOiBarcode));
                }
                textEdit1.Text = strOiBarcode;
                if (orderInfoList == null || isAll==true)
                {
                    orderInfoList = new List<OrderInfo_Info>();
                    isAll = false;
                }
                
                if (orderInfoList.Count == 0)
                {
                    orderInfoList = _bll.ReceiveReBarCode(strOiBarcode, out  shopInfo);
                    if (orderInfoList == null)
                    {
                        MessageBox.Show("产品未进格子", "提示", MessageBoxButtons.OK, MessageBoxIcon.Information);
                        return;
                    }
                    if (orderInfoList.Count == 0)
                    {
                        MessageBox.Show("产品不存在", "提示", MessageBoxButtons.OK, MessageBoxIcon.Information);
                        return;
                    }
                    SetProductStatus(strOiBarcode);
                    gridControlProduct.DataSource = orderInfoList;
                    this.textEditShopname.Text = shopInfo.ShopName;
                    textEditNum.Text = orderInfoList.Count().ToString(); ;


                }
                else
                {
                    SetProductStatus(strOiBarcode);
                }
                this.textEdit1.Text = "";


            }
          
        }
        void SetProductStatus(string inStrBarcode)
        {
            if (orderInfoList.Count > 0)
            {
                OrderInfo_Info oiInfo = orderInfoList.Where(p => p.OrderProductCode == inStrBarcode).FirstOrDefault();
                if (oiInfo != null)
                {
                    if (oiInfo.IsCheck)
                    {
                        MessageBox.Show("此产品已复检过，请注意！","注意",MessageBoxButtons.OK,MessageBoxIcon.Warning);
                    }
                    else
                    {
                        oiInfo.IsCheck = true;
                    }
                }
                else
                {
                    MessageBox.Show("此产品不属于该商户", "提示", MessageBoxButtons.OK, MessageBoxIcon.Error);
                    return;
                }
            }
            else
            {
                MessageBox.Show("产品不存在", "提示", MessageBoxButtons.OK, MessageBoxIcon.Information);
                return;
            }
            int nowNum=orderInfoList.Where(p => p.IsCheck == true).Count();
            textEditInNum.Text = nowNum.ToString();
           
            gridControlProduct.RefreshDataSource();
            if (nowNum == orderInfoList.Count)
            {
                isAll = true;
                MessageBox.Show("质检完成", "提示", MessageBoxButtons.OK, MessageBoxIcon.Information);
                //return;
            }
        }
        bool isAll = false;
        private void gridViewProduct_CustomDrawCell(object sender, DevExpress.XtraGrid.Views.Base.RowCellCustomDrawEventArgs e)
        {
            //if (e.RowHandle >= 0)
            //{
            //    if (e.Column.Name == "IsCheck")
            //    {
            //        OrderInfo_Info INFO = ((OrderInfo_Info)gridViewProduct.GetRow(e.RowHandle));
            //        if (INFO.IsCheck)
            //        {
            //            e.DisplayText = "是";      
            //        }
            //        else
            //        {
            //            e.DisplayText = "否";
            //        }
            //    }

            //}
        }

        private void FrmReCheck_Load(object sender, EventArgs e)
        {
            //this.Text = Int32.MaxValue.ToString() + "|" + Int32.MinValue.ToString() ;

        }

        private void gridViewProduct_RowStyle(object sender, DevExpress.XtraGrid.Views.Grid.RowStyleEventArgs e)
        {
            if (e.RowHandle >= 0)
            {
                OrderInfo_Info INFO = ((OrderInfo_Info)gridViewProduct.GetRow(e.RowHandle));
                if (INFO.IsCheck == true)
                {
                    e.Appearance.BackColor = Color.LightGreen;
                }
            }
        }

        private void simpleButton1_Click(object sender, EventArgs e)
        {
            orderInfoList = new List<OrderInfo_Info>();
            textEdit1.Text = "";
            textEditShopname.Text = "";
            textEditNum.Text = "";
            textEditInNum.Text = "";
            gridControlProduct.DataSource = null;
            gridControlProduct.RefreshDataSource();
        }
    }
}
